// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.cdc;

import org.dengues.core.resource.DenguesProjectFactory;
import org.dengues.model.project.CDCConnType;
import org.dengues.ui.PropertiesUiUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-4 qiang.zhang $
 * 
 */
public class CDCSettingPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

    private Composite propsComp;

    /**
     * Qiang.Zhang.Adolf@gmail.com CDCSettingPreferencePage constructor comment.
     */
    public CDCSettingPreferencePage() {
        super();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createContents(Composite parent) {
        Font font = parent.getFont();
        Composite content = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        content.setLayout(layout);
        GridData data = new GridData(GridData.FILL_BOTH);
        content.setLayoutData(data);
        content.setFont(font);

        Label label = new Label(content, SWT.LEFT);
        label.setText("Change Data Capture");
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        label.setLayoutData(data);
        Font font2 = new Font(null, font.getFontData()[0].getName(), 10, SWT.BOLD);
        label.setFont(font2);
        CDCSettingsViewer viewer = new CDCSettingsViewer(content, SWT.NONE);
        EList<CDCConnType> cdcConns = DenguesProjectFactory.getDenguesProject().getProjectType().getCdcConns();
        viewer.setInput(cdcConns);

        propsComp = new Composite(content, SWT.NONE);
        GridLayout gridLayout = new GridLayout(2, false);
        propsComp.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        propsComp.setLayoutData(gridData);
        viewer.getTableViewer().addSelectionChangedListener(new ISelectionChangedListener() {

            public void selectionChanged(SelectionChangedEvent event) {
                StructuredSelection selection = (StructuredSelection) event.getSelection();
                if (selection != null && selection.size() == 1) {
                    CDCConnType fe = (CDCConnType) selection.getFirstElement();
                    PropertiesUiUtils.createPropertiesControls(null, propsComp, fe.getConnSet());
                }
            }

        });
        return content;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench) {

    }

}
